/*
 * ethernet.h
 *
 *  Created on: May 25, 2018
 *      Author: yuantao
 */

#ifndef ETHERNET_H_
#define ETHERNET_H_

#include "init.h"
#ifdef _WIN32
#include "windows.h"
#else
#include "sys/socket.h"
#define NO_ERROR 0
#endif

#define BYTES_SEND_ONCE 4096
#define MAX_BYTES_RECV_ONCE 8208

typedef enum
{
	PROTOCOL_UDP,
	PROTOCOL_TCP
}IP_PROTOCOL;

typedef int (*recvCallBack)(char * buffRecv,unsigned int LengthRecv);

class CEthernet
{
private:
#ifdef _WIN32
	SOCKET m_sockRemote;
	SOCKET m_sockLocal;
#else
	int m_sockRemote;
	int m_sockLocal;
#endif
    connection_config m_stConnConfig;
    int m_isClient;// is client or server
    int m_ip_protocol;//is UDP or TCP
#ifdef _WIN32
	HANDLE readUdpData_ThreadHandle;
#else
	pthread_t readUdpData_ThreadHandle;
#endif
    

public:
    void setConnectionConfig(connection_config * pstConnConfig);
    int startConnection(int isClient,int ip_protocol);
    int stopConnection(void);
    CEthernet();
    ~CEthernet();

    int recvUdpData(char * buffOut,unsigned int maxLengthIn);
    int sendUdpData(const char * buffIn,unsigned int buffLen);

    ThreadRunFlag recvThreadRunningFlag;
    int recvThreadExitVal;

    void joinRecvThread(void);
    recvCallBack pfuncRecvCallBack; 
    
    void setRecvCallBackFunc(recvCallBack funcRecvCallBack);
};

#ifdef _WIN32
DWORD WINAPI   threadFunc_recvUdpData(LPVOID arg);
#else
void *threadFunc_recvUdpData(void *arg);
#endif


#endif /* ETHERNET_H_ */
